package com.request.strategy;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 支持的参数注解类型
 *
 * @author yuanqinglong
 * @since 2021/10/18 11:52
 */
@Component
public class ParamProcessorStrategyFactory implements BeanPostProcessor {

    private static Map<Class<?>, RequestParamTypeProcess> requestParamTypeProcessorMap;

    public ParamProcessorStrategyFactory(List<RequestParamTypeProcess> requestParamTypeProcesses) {
        ParamProcessorStrategyFactory.requestParamTypeProcessorMap = requestParamTypeProcesses.stream().filter(requestParamTypeProcess -> Objects.nonNull(requestParamTypeProcess.paramType())).collect(Collectors.toMap(RequestParamTypeProcess::paramType, Function.identity()));
    }

    public static RequestParamTypeProcess getRequestParamTypeProcessor(final Class<?> paramAnnotationType) {
        if (requestParamTypeProcessorMap.containsKey(paramAnnotationType)){
            return requestParamTypeProcessorMap.get(paramAnnotationType);
        }
        throw new RuntimeException("没有匹配的注解解析器");
    }


    public boolean containsAnnotationType(final Class<?> paramAnnotationType) {
        return requestParamTypeProcessorMap.containsKey(paramAnnotationType);
    }

}
